package com.wonyesoft.wuliu.acticity;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.R.integer;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.TabHost;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;
import com.wonyesoft.wuliu.AppContent;
import com.wonyesoft.wuliu.R;
import com.wonyesoft.wuliu.acticity.base.BaseActivity;
import com.wonyesoft.wuliu.api.GagApi;
import com.wonyesoft.wuliu.model.CargoInfo;
import com.wonyesoft.wuliu.vo.JsonRequest;
import com.wonyesoft.wuliu.vo.JsonResponse;
import com.wonyesoft.wuliu.vo.State;


public class MyOrderListActivity extends BaseActivity implements
		OnClickListener {

	private TabHost mTabHost;
	private RadioGroup mRadioGroup;
	private TextView title, head_right;
	private ImageView head_left;
	private ListView mylist;
	private SimpleAdapter adapter;
	private List<Map<String, Object>> list;
	private TextView myorder_content_flowid, myorder_content_tab,
			myorder_content_time, myorder_content_mtext, myorder_content_rmb,
			myorder_content_qitext, myorder_content_zhongtext,
			listviewGetOrderid;
	String listviewFlowId;
	private Gson gson = new Gson();
	private HttpUtils http = new HttpUtils();
	private RequestParams params;
	private AppContent app;
	private PreferencesCookieStore preferencesCookieStore;
	private List<CargoInfo> sourceDateList;
	private String flowStatus = null;

	@SuppressLint("NewApi")
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myorder);
		preferencesCookieStore = app.getPreferencesCookieStore();
		// title = (TextView) findViewById(R.id.head_title);
		// head_left = (ImageView) findViewById(R.id.head_left);
		// title.setText("货单管理");
		// head_left.setOnClickListener(this);
		// head_left.setVisibility(0);
		
		flowStatus = getIntent().getStringExtra("flowstatus");
		getPlanList();
	}

	Bundle bundle = new Bundle();

	private void init() {
		mylist = (ListView) findViewById(R.id.myorder_listview);
		mylist.setDividerHeight(0);

		adapter = new SimpleAdapter(this, getData(),
				R.layout.activity_myorder_conent, new String[] {
						"myorder_content_flowid", "myorder_content_tab",
						"myorder_content_time", "myorder_content_mtext",
						"myorder_content_rmb", "myorder_content_qitext",
						"myorder_content_zhongtext" }, new int[] {
						R.id.myorder_content_flowid, R.id.myorder_content_tab,
						R.id.myorder_content_time, R.id.myorder_content_mtext,
						R.id.myorder_content_rmb, R.id.myorder_content_qitext,
						R.id.myorder_content_zhongtext });

		mylist.setAdapter(adapter);
		mylist.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				System.out.println("点了那个坐标:" + position);
				HashMap<String, Object> map = (HashMap<String, Object>) mylist
						.getItemAtPosition(position);
				System.out.println("nv" + map.get("myorder_content_flowid")
						+ "");
				// System.out.println("cargoflow:" + cargo.getFlowId());
				bundle.putString("listid", map.get("myorder_content_flowid")
						+ "");
				openActivity(OrderDetails.class, bundle);
			}

		});
		myorder_content_flowid = (TextView) findViewById(R.id.myorder_content_flowid);
		myorder_content_tab = (TextView) findViewById(R.id.myorder_content_tab);
		myorder_content_time = (TextView) findViewById(R.id.myorder_content_time);
		myorder_content_mtext = (TextView) findViewById(R.id.myorder_content_mtext);
		myorder_content_rmb = (TextView) findViewById(R.id.myorder_content_rmb);
		myorder_content_qitext = (TextView) findViewById(R.id.myorder_content_qitext);
		myorder_content_zhongtext = (TextView) findViewById(R.id.myorder_content_zhongtext);

	}

	private List<Map<String, Object>> getData() {
		list = new ArrayList<Map<String, Object>>();

		for (CargoInfo goods : sourceDateList) {

			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("myorder_content_flowid", goods.getFlowId());
			// System.out.println("goods.getFlowId()"+goods.getFlowId());
			map.put("myorder_content_tab", "订单号:"+goods.getFlowId());
			// map.put("myorder_content_time", "2015 b0903");
			map.put("myorder_content_time", "发布时间:"+goods.getCreateTime());
			// System.out.println(goods.getCreateTime());
			map.put("myorder_content_mtext", goods.getDistance());
			map.put("myorder_content_rmb", goods.getTransPrice());
			map.put("myorder_content_qitext", goods.getReAddress());
			map.put("myorder_content_zhongtext", goods.getSdAddress());
			list.add(map);
		}

		return list;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.head_left:
			finish();
			break;
		default:
			break;
		}
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		int i = 0;
		System.out.println("onresume" + i++) ;
	}

	private void getPlanList() {
		MyOrderListActivity.this.showLoadingDialog("数据载入中...");
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("flowStatus", flowStatus);
		JsonRequest jr = JsonRequest.getInit();
		jr.setData(dataMap);
		String json = gson.toJson(jr);
		params = new RequestParams();
		params.addBodyParameter("jsonParam", json);
		http.configCookieStore(AppContent.getPreferencesCookieStore());
		http.send(HttpRequest.HttpMethod.POST, GagApi.QueryOnlineOrder, params,
				new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						MyOrderListActivity.this.dismissLoadingDialog();
						//MyOrderListActivity.this.dismissLoadingDialog();
						System.out.println("进入添加订单lists");
						System.out.println("list:" + responseInfo.result);
						Gson gson2 = new Gson();
						JsonResponse jrs = gson2.fromJson(responseInfo.result,
								JsonResponse.class);
						State state = jrs.getState();

						// System.out.println(preferencesCookieStore);
						if (state.getCode().equals(State.SUCEECCODE)) {

							// 存用户登录信息
							// System.out.println(responseInfo.result);
							System.out.println("进来了");
							System.out.println(jrs.getParam("rows") + "");
							Type type = new TypeToken<List<CargoInfo>>() {
							}.getType();
							try {
								System.out.println("rows"
										+ new JSONObject(responseInfo.result)
												.getJSONObject("data")
												.getJSONArray("rows") + "");
								sourceDateList = gson2.fromJson(
										new JSONObject(responseInfo.result)
												.getJSONObject("data")
												.getJSONArray("rows")
												+ "", type);
							} catch (JsonSyntaxException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

							init();
							/*
							 * if (gson2 != null) { Type type = new
							 * TypeToken<List<CargoInfo>>() { }.getType();
							 * sourceDateList = gson2.fromJson(
							 * jrs.getParam("rows") + "", type); init(); }
							 */
							System.out.println(jrs.getParam("rows"));

						} else {

							System.out.println(state.getMsg());
							if (state.getCode().equals(State.UNLOGIN)) {
								openActivity(UserLoginActivity.class);
								finish();

							}
						}
						// ToastUtils.showLong(state.getMsg());}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						// testTextView.setText(error.getExceptionCode() + ":" +
						// msg);
						MyOrderListActivity.this.dismissLoadingDialog();
						System.out.println(msg);
					}
				});

	}
}
